/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.view;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.border.LineBorder;
import vical.controller.IconManager;
import vical.model.DayCardModel;
import vical.model.Entry;
import vical.model.Internationalization;
import vical.utility.AnimationDriver;
import vical.utility.Functions;

/**
 * This is the Panel to represent a Day Card
 * 
 * @author Can
 * @author Pooya
 * @author Monica
 * @author Flora
 */
public class DayCard extends JPanel implements IDayCard {

    private JPanel datePanel;
    private JPanel entryPanel;
    private JLabel dateLabel;
    private DayCardModel dayCardModel;
    private MonthView monthView;
    private final Color datePanelBackColor = Color.WHITE;
    private final Color regularEntryPanelColor = new Color(245, 245, 245);
    private final Color otherEntryPanelColor = new Color(245, 245, 245);
    private final Color todayColor = new Color(204, 255, 255);
    private Color entryPanelColor;

    private DayCard() {
    }

    public DayCard(DayCardModel model, MonthView monthView) {
        this.dayCardModel = model;
        this.monthView = monthView;

        initComponents();
    }

    private void initComponents() {
        dateLabel = new JLabel(dayCardModel.getDateNumber() + "");
        datePanel = new JPanel();

        entryPanel = new JPanel();
        entryPanel.setToolTipText(Internationalization.getString("tooltip_daycard_add_task"));

        entryPanel.addMouseListener(new MouseAdapter() {

            /**
             * On mouse double click it opens Entry in EntryDialog
             *
             */
            @Override
            public void mouseClicked(MouseEvent e) {
                int numberOfClicks = e.getClickCount();
                if (numberOfClicks == 2) {
                    int date = dayCardModel.getDateNumber();
                    int month = dayCardModel.getMonth();
                    int year = dayCardModel.getYear();
                    Entry entry = new Entry();
                    Calendar tempCal = Calendar.getInstance();
                    tempCal.set(Calendar.YEAR, year);
                    tempCal.set(Calendar.MONTH, month);
                    tempCal.set(Calendar.DATE, date);
                    entry.setStartTime(tempCal);
                    entry.setEndTime(tempCal);

                    EntryDialog entryDialog = new AddEntryDialog(entry);
                    entryDialog.setVisible(true);
                    getMonthView().notifyEntryChanged();
                    getMonthView().refresh();
                }
                super.mouseClicked(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                repaint();
                setSelected(false);
                super.mouseExited(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                monthView.selectDayCard(DayCard.this);
                super.mouseEntered(e);
            }
        });

        JLabel iconLabel = new JLabel();

        datePanel.setLayout(new BorderLayout());
        datePanel.add(BorderLayout.WEST, iconLabel);
        datePanel.add(BorderLayout.EAST, dateLabel);
        datePanel.setBackground(datePanelBackColor);

        if (dayCardModel.getDateType() == DayCardModel.DayType.RegularDay) {
            dateLabel.setFont(new Font(dateLabel.getFont().getName(), Font.PLAIN, dateLabel.getFont().getSize() - 2));
            dateLabel.setBorder(new LineBorder(Color.WHITE, 2));
            dateLabel.setForeground(Color.BLACK);


            // Entries
            entryPanel.setBackground(regularEntryPanelColor);
            entryPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            // Load all entries
            for (Entry entry : dayCardModel.getEntries()) {
                JEntryLabel entryLabel = new JEntryLabel(this, entry);
                entryLabel.setPreferredSize(new Dimension(100, 13));
                entryPanel.add(entryLabel);

                // Set Icon
                if (entry.getSpecialEvents().equals("Birthday") || entry.getSpecialEvents().equals("födelsedag")) {
                    ImageIcon icon = IconManager.getImageIcon(IconManager.Icons.Birthday);
                    iconLabel = new JLabel(Functions.scale(this, icon.getImage(), 19.0 / icon.getIconHeight()), SwingConstants.CENTER);
                } else if (entry.getSpecialEvents().equals("Anniversary") || entry.getSpecialEvents().equals("jubileum")) {
                    ImageIcon icon = IconManager.getImageIcon(IconManager.Icons.Anniversary);
                    iconLabel = new JLabel(Functions.scale(this, icon.getImage(), 19.0 / icon.getIconHeight()), SwingConstants.CENTER);
                } else if (entry.getSpecialEvents().equals("Tour")) {
                    ImageIcon icon = IconManager.getImageIcon(IconManager.Icons.Tour);
                    iconLabel = new JLabel(Functions.scale(this, icon.getImage(), 19.0 / icon.getIconHeight()), SwingConstants.CENTER);
                } else if (entry.getSpecialEvents().equals("Wedding") || entry.getSpecialEvents().equals("bröllop")) {
                    ImageIcon icon = IconManager.getImageIcon(IconManager.Icons.Wedding);
                    iconLabel = new JLabel(Functions.scale(this, icon.getImage(), 19.0 / icon.getIconHeight()), SwingConstants.CENTER);
                } else if (entry.getSpecialEvents().equals("Hiking") || entry.getSpecialEvents().equals("Fotvandring")) {
                    ImageIcon icon = IconManager.getImageIcon(IconManager.Icons.Hiking);
                    iconLabel = new JLabel(Functions.scale(this, icon.getImage(), 19.0 / icon.getIconHeight()), SwingConstants.CENTER);
                } else if (entry.getSpecialEvents().equals("Select...") || entry.getSpecialEvents().equals("Ingen"))
                    iconLabel.setVisible(false);
                iconLabel.setBorder(BorderFactory.createLineBorder(Color.gray));
                datePanel.add(BorderLayout.WEST, iconLabel);

            }
            this.setBorder(new LineBorder(Color.WHITE, 1));

        }

        if (dayCardModel.getDateType() == DayCardModel.DayType.OtherDays) {
            dateLabel.setFont(new Font(dateLabel.getFont().getName(), Font.PLAIN, dateLabel.getFont().getSize() - 2));
            dateLabel.setBorder(new LineBorder(Color.WHITE, 2));
            dateLabel.setForeground(Color.LIGHT_GRAY);
            entryPanel.setBackground(otherEntryPanelColor);
            this.setBorder(new LineBorder(Color.WHITE, 1));
        }

        this.setLayout(new BorderLayout());
        this.add(BorderLayout.NORTH, datePanel);
        this.add(BorderLayout.CENTER, entryPanel);

        if (dayCardModel.isToday()) {
            dateLabel.setBorder(new LineBorder(Color.ORANGE, 2));
        }

        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                Object o = e.getSource();
                if (o instanceof DayCard) {
                    monthView.selectDayCard((DayCard) o);
                }
                super.mouseEntered(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setSelected(false);
                super.mouseExited(e);
            }
        });
    }

    public void highlightEntryiesByCategory(String category) {

        for (Entry entry : dayCardModel.getEntries()) {
            if (entry.getCategory().equals(category)) {
                highlightDayCards();
                break;
            }
        }
    }

    public ArrayList<Entry> getEntries() {
        return dayCardModel.getEntries();
    }

    private void highlightDayCards() {
        AnimationDriver.highlight(this.entryPanel, 200, 2);
    }

    public void setSelected(boolean bSelected) {
        if (bSelected) {
            if (entryPanelColor == null) {
                entryPanelColor = entryPanel.getBackground();
            }
            entryPanel.setBackground(todayColor);
        } else {
            this.setBorder(new LineBorder(Color.WHITE, 1));
            if (entryPanelColor != null) {
                entryPanel.setBackground(entryPanelColor);
            }
        }
    }

    public MonthView getMonthView() {
        return monthView;
    }
}
